import { credit } from "../../../../js/modules/credit.js";
import { orderPay } from "../../../../js/modules/pay.js";
import { forms, storage, util,api } from '../../../../js/util';
import authorize from '../../../../js/modules/authorize.js'
import BasePage from '../../../../js/common/base-page'
const app = getApp();
BasePage({
  pageKey:"waitBillList",

  /**
   * 页面的初始数据
   */
  data: {
    page: {
      PageIndex: 1,
      PageSize: 1000,
      keywords: "",
      sortField: "AddTime",
      sortDirect: "desc"
    },
    list: [],
    hasMoreData: true,
    repayAmount:0.00,
    arr_bill:[],
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      accountId: options.accountId
    });
    that.loadList();
  },
  /**
     * 加载列表
     *  
  */
  loadList() {
    var that = this;
    var page = this.data.page;
    credit.load_list(page, that.data.accountId, function (content) {
      var hasMoreData = content.count > (page.PageIndex) * page.PageSize;
      var index = that.data.list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (page.PageIndex > 1) {
        data = that.data.list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        list: data,
        hasMoreData: hasMoreData,
        isLoadComplete: true
      });
    })
  },
  /**
   * 计算
   */
  caculate:function(){
    var that=this;
    var repayAmount = 0,arr_bill=[];
    for (var i = 0; i < that.data.list.length; i++) {
      if (that.data.list[i].Checked) {
        var item = that.data.list[i];
        repayAmount += item.RepayAmount - item.AccountPaid - item.RefundAmount + item.ShouldOverdueAmount;
        arr_bill.push(item.Id);
      }
    }
    that.setData({
      list: that.data.list,
      repayAmount: repayAmount,
      arr_bill: arr_bill
    });
  },
  /**
  * 单个选择事件
  */
  onSelectSingleEvent: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.list[index].Checked = !(that.data.list[index].Checked||false);
    that.caculate();
  },
  /**
   * 全选事件
   */
  onSelectAllEvent: function (e) {
    var that = this;
    that.data.select_all_status = (that.data.select_all_status == 1 ? 0 : 1);
    for (var i = 0; i < that.data.list.length; i++) {
      if (that.data.list[i].Status == 0 || that.data.list[i].Status == 3){
        that.data.list[i].Checked = that.data.select_all_status;
      }
    }
    that.caculate();
  },
  /**
 * 按账单还款
 */
  onRepayWithBillEvent: function () {
    if (this.data.arr_bill == null || this.data.arr_bill.length==0){
      util.alert("请选择要还款的账单");
      return;
    }
    credit.pay_bill(0, this.data.accountId, this.data.arr_bill.join(","),0, this.data.repayAmount, function (content) {
      orderPay.findPayOrderData(content.PaySign, "cashier");
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})